package com.hxs.learn.action.core;

import java.util.List;

import com.hxs.learn.constants.core.web.ReqConst;
import com.hxs.learn.constants.core.web.view.ViewNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.hxs.learn.common.entity.TreeJson;
import com.hxs.learn.common.result.Result;
import com.hxs.learn.common.web.LoginInfo;
import com.hxs.learn.common.web.annotation.Login;
import com.hxs.learn.entity.core.SysMenuPermission;
import com.hxs.learn.service.core.SysMenuPermissionService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author huxinsheng
 * @date 2018/3/30 16:49
 */
@RestController
@RequestMapping("core/menu/permission")
public class MenuPermissionAction extends AbstractViewAction {
    private final SysMenuPermissionService menuPermissionService;

    @Autowired
    public MenuPermissionAction(SysMenuPermissionService menuPermissionService) {
        this.menuPermissionService = menuPermissionService;
    }

    @ApiOperation(
        value = "修改菜单权限数据",
        notes = "菜单管理时显示修改菜单权限数据"
    )
    @PostMapping(ReqConst.REQ_MODIFY_DATA)
    @ResponseBody
    public Result modifyRoleMenuData(@Login LoginInfo loginInfo, SysMenuPermission sysMenuPermission) {
        return menuPermissionService.save(loginInfo, sysMenuPermission);
    }

    @ApiOperation(
        value     = "编辑菜单权限页面",
        notes     = "菜单管理时显示编辑菜单权限页面"
    )
    @ApiImplicitParams({ @ApiImplicitParam(
        name      = "menuId",
        value     = "菜单ID",
        required  = true,
        dataType  = "String",
        paramType = "query"
    ) , })
    @GetMapping(ReqConst.REQ_MODIFY_MENU_PERMISSION_DIALOG)
    public ModelAndView modifyRoleMenuPermissionDialog(Model model, String menuId) {
        model.addAttribute(ReqConst.PARAM_MENU_ID, menuId);

        return createModelAndView(ViewNames.PAGE_MENU_PERMISSION_MODIFY);
    }

    @ApiOperation(
        value     = "加载菜单权限数据",
        notes     = "菜单管理时显示加载菜单权限数据"
    )
    @ApiImplicitParams({ @ApiImplicitParam(
        name      = "menuId",
        value     = "菜单ID",
        required  = true,
        dataType  = "String",
        paramType = "query"
    ) , })
    @GetMapping("getMenuPermissionData")
    @ResponseBody
    public Result getMenuPermissionData(String menuId) {
        List<TreeJson<Object>> roleMenuPermissionTreeJson = menuPermissionService.getMenuPermissionTreeJson(menuId);

        return Result.ok(roleMenuPermissionTreeJson);
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
